package cqupt.spark_log.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cqupt.spark_log.utils.ECUtil;

public class Stage {
	private String stageId;
	private String stageType;
	private Date startTime;
	private Date endTime;
	private double ec;
	private String jobId;
	private List<Task> tasks = new ArrayList<Task>();
	
	public Stage(String stageId, String stageType, Date startTime, String jobId) {
		this.stageId = stageId;
		this.stageType = stageType;
		this.startTime = startTime;
		this.jobId = jobId;
	}
	
	public String getStageId() {
		return stageId;
	}
	public void setStageId(String stageId) {
		this.stageId = stageId;
	}
	public String getStageType() {
		return stageType;
	}
	public void setStageType(String stageType) {
		this.stageType = stageType;
	}
	
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public void addTask(Task task) {
		tasks.add(task);
	}
	public String getJobId() {
		return jobId;
	}
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}
	public List<Task> getTasks() {
		return tasks;
	}
	
	
	public double getEc() {
		if (ec<=0) {
			for (int i=0; i<tasks.size(); i++) {
				Task task = tasks.get(i);
				ec += task.getEc();
			}
		}
		ec = ECUtil.get4point(ec);
//		System.out.println("stage"+this.stageId+"能量为["+ec+"]");
		return ec;
	}

	public void setEc(double ec) {
		this.ec = ec;
	}

	@Override
	public String toString() {
		return "Stage [stageId=" + stageId + ", stageType=" + stageType + ", startTime=" + startTime + ", endTime="
				+ endTime + ", ec=" + ec + ", jobId=" + jobId + "]";
	}

	public long getRuntime() {
		//stage里的task是并行的,故求得并行任务中运行时间的最大值，作为当前阶段的运行时间
		long result = Long.MIN_VALUE;
		for (Task task: tasks) {
			long taskTime = task.getExecutorRunTime();
//			long taskTime = task.getExecutorCpuTime();
//			long taskTime = task.getRunTime();
			if (taskTime>result) {
				result = taskTime;
			}
		}
		return result;
	}
}
